package com.xywang.service.provider;

import com.xywang.dao.BaseDao;
import com.xywang.dao.bill.BillDao;
import com.xywang.dao.bill.BillDaoImpl;
import com.xywang.dao.provider.ProviderDao;
import com.xywang.dao.provider.ProviderDaoImpl;
import com.xywang.pojo.Provider;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProviderServiceImpl implements ProviderService{
    private ProviderDao providerDao;
    private BillDao billDao;
    public ProviderServiceImpl() {
        providerDao = new ProviderDaoImpl();
        billDao = new BillDaoImpl();
    }
    // 获取供应商列表
    @Override
    public List<Provider> getProviderListService(String proCode, String proName) {
        Connection connection = null;
        List<Provider> providerList = null;
        try {
            connection = BaseDao.getConnection();
            providerList = providerDao.getProviderListDao(connection, proCode, proName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return providerList;
    }
    // 增加供应商
    @Override
    public boolean addProService(Provider provider) {
        boolean flag = false;
        Connection connection = null;
        int i = 0;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            i = providerDao.addProDao(connection, provider);
            connection.commit();
            if (i > 0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            //在service层进行connection连接的关闭
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    // 通过proId删除Provider
    @Override
    public int deleteProviderByIdService(String delId) {
        Connection connection = null;
        int billCount;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            // 获取供应商为delId的订单总数
            billCount = billDao.getBillCountByProviderId(connection, delId);
            if (billCount == 0) {
                // 为0，表示没有任何订单和该供应商有关，可以删除，执行删除
                providerDao.deleteProviderById(connection, delId);
            }
            connection.commit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // sql执行错误，将订单总数设为-1，表示删除失败
            billCount = -1;
            try {
                connection.rollback();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        // 假如billCount既不是0，也不是-1，
        // 则billCount这个数代表的是和该供应商有关联的订单总数
        return billCount;
    }

    @Override
    public Provider getProviderByIdService(String id) {
        Provider provider;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            provider = providerDao.getProviderById(connection, id);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            provider = null;
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return provider;
    }

    @Override
    public boolean modifyService(Provider provider) {
        Connection connection = null;
        boolean flag = false;
        int i = 0;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);// 开启事务
            i = providerDao.modifyDao(connection, provider);
            connection.commit();
            if (i > 0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("ProviderServiceImpl---modifyService 修改失败，回滚");
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
}
